package com.ruoyi.project.management.delay.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.management.delay.domain.TaskDelay;
import com.ruoyi.project.management.delay.mapper.ManagementDelayMapper;
import com.ruoyi.project.management.delay.mapper.ManagementTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ManagementDelayServiceImpl implements ManagementDelayService {

    @Autowired
    private ManagementDelayMapper managementDelayMapper;


    /*@Autowired
    private ManagementTaskMapper managementTaskMapper;*/


    @Override
    public List<TaskDelay> selectMore(TaskDelay taskDelay) {
        return managementDelayMapper.selectMore(taskDelay);
    }


    @Override
    public String checkDelayTaskIdUnique(TaskDelay taskDelay) {
        TaskDelay info = managementDelayMapper.checkDelayTaskIdUnique(taskDelay.getTask_id());
        if (StringUtils.isNotNull(info))
        {
            return UserConstants.DELAY_TASKID_UNIQUE_AND_EXIST;
        }
        return UserConstants.DELAY_TASKID_NOT_UNIQUE_AND_EXIST;
    }

    @Override
    public Integer insert(TaskDelay taskDelay) {
        return managementDelayMapper.insert(taskDelay);
    }


    @Override
    public TaskDelay selectDelayById(Integer delay_Id) {
        return managementDelayMapper.selectDelayById(delay_Id);
    }

    @Override
    @Transactional
    public Integer update(TaskDelay taskDelay) {

        System.out.println(taskDelay);

        /*taskDelay.setCreateBy(ShiroUtils.getLoginName());*/
        //当延期任务被批准,并且有约定时间时,修改任务的截止时间为约定时间
        /*System.out.println(delay.toString());*/
        /*if(taskDelay.getPass() == 1 && taskDelay.getApplication_time() != null){
            System.out.println("+++++++++++++++++++Yes++++++++++++++++++");
            managementTaskMapper.updateByEndTime(taskDelay.getAppointed_time(),taskDelay.getTask_id());
        }
        ShiroUtils.clearCachedAuthorizationInfo();*/

        return managementDelayMapper.update(taskDelay);
    }

    @Override
    public int delete(String ids) {
        Long[] delayTaskIDs = Convert.toLongArray(ids);

        return managementDelayMapper.delete(delayTaskIDs);
    }
}
